<template>
  <div class="bd_block">
    <div
      v-for="(building, index) in buildings"
      :key="index"
      class="bd_block_card"
      :style="{
        backgroundColor: `${buildingMap[building.building_type]['color']}`,
      }"
    >
      <div v-if="!building.isEditing" class="bd_block_card_wh">
        <div class="bd_block_card_wh_header">
          <a-icon
            class="bd_block_card_wh_header_edit"
            type="edit"
            @click.stop="edit(index, building)"
          />
          <!-- <a-icon
            class="bd_block_card_wh_header_delete"
            type="close"
            @click="deleteSpace(index, building)"
          /> -->
        </div>
        <div
          class="bd_block_card_wh_body"
          @click="buildingSelect({ building_id: building._id, building_name: building.building_name})"
        >
          <a class="bd_block_card_wh_body_lk">{{ building.building_name }}</a>
        </div>
        <div class="bd_block_card_wh_footer">
          <span class="bd_block_card_wh_footer_tag">{{
            building.building_type
          }}</span>
        </div>
        <div class="bd_block_card_wh_icon">
          <a-icon :type="buildingMap[building.building_type]['icon']" />
        </div>
      </div>
      <div v-else class="bd_block_card_wh">
        <div class="bd_block_card_wh_header">
          <a-icon 
            class="bd_block_card_wh_header_edit"
            type="check" 
            @click="endEdit(index, building)"
          />
        </div>
        <div
          class="bd_block_card_wh_body"
        >
          <input
            type="text"
            placeholder="建筑名名不能为空"
            v-model="building.building_name"
            class="bd_block_card_wh_body_input"
          />
        </div>
        <div class="bd_block_card_wh_footer">
          <select 
            class="bd_block_card_wh_footer_select"
            v-model="building.building_type" 
            @change="changeType"
          >
            <option
              class="bd_block_card_wh_footer_select_op"
              v-for="buildingType in buildingTypes"
              :key="buildingType"
              :value="buildingType">
              {{ buildingType }}
            </option>
          </select>
        </div>
        <div class="bd_block_card_wh_icon">
          <a-icon :type="buildingMap[building.building_type]['icon']" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axiosInstance from "@/utils/axios";

export default {
  props: ["buildingProp"],
  data() {
    return {
      buildingMap: {
        教学楼: { name: "教学楼", color: "#96BFFF", icon: "home" },
        实验楼: { name: "实验楼", color: "#FFDB5C", icon: "experiment" },
        办公楼: { name: "办公楼", color: "#9FE6B8", icon: "bank" },
        图书馆: { name: "图书馆", color: "#9FE6B8", icon: "bank" },
        其他: { name: "其他", color: "#ff9f7f", icon: "question" },
      },
      buildingTypes: ["教学楼", "实验楼", "办公楼", "图书馆", "其他"],
      checkedList: [],
    };
  },
  watch: {
  },
  computed: {
    buildings() {
      return this.buildingProp.map((building) => {
        switch (building.building_type) {
          case "classroom":
            building.building_type = "教学楼";
            break;
          case "office":
            building.building_type = "办公楼";
            break;
          case "lab":
            building.building_type = "实验楼";
            break;
          case "library":
            building.building_type = "图书馆";
            break;
          case "others":
            building.building_type = "其他";
            break;
        }
        return {
          ...building,
          isEditing: false,
        };
      });
    },
  },
  methods: {
    onChange(e, value) {
      if (e.target.checked) {
        this.checkedList.push(value);
      } else {
        let index = this.checkedList.findIndex((item) => {
          return item === value;
        });
        //删除元素
        this.checkedList.splice(index, 1);
      }
      console.log(this.checkedList);
    },
    edit(index, building) {
      this.buildings[index].isEditing = true;
      this.$forceUpdate();
    },
    async endEdit(index, building) {
      let url = `pc/v1/building/${building._id}`;
      try {
        await axiosInstance.patch(url, building);
        this.$message.info("编辑成功");
        this.buildings[index].isEditing = false;
        this.$forceUpdate();
      } catch (err) {
        console.log(err);
        this.$message.error("编辑失败");
      }
    },
    async deleteSpace(index, building) {
      let url = `pc/v1/building/${building._id}`;
      try {
        await axiosInstance.delete(url, building);
        this.$message.info("删除成功");
        this.buildings.splice(index, 1);
        this.$forceUpdate();
      } catch (err) {
        console.log(err);
        this.$message.error("删除失败");
      }
    },
    buildingSelect(data) {
      this.$emit("tellFather", data);
    },
    changeType() {
      this.$forceUpdate();
    },
  },
  mounted() {},
};
</script>

<style scoped>
.my-statistic-title {
  margin-bottom: 10px;
  color: #fff;
  font-size: 20px;
  letter-spacing: 3px;
}

.btn-area {
  padding: 20px 0 20px;
  display: flex;
  justify-content: space-between;
}

.Card_Info {
  padding-top: 20px;
}
</style>